<template>
  <el-drawer v-model="drawerVisible" :destroy-on-close="true" size="450px" :title="`${drawerProps.title}角色`">
    <el-form
      ref="ruleFormRef"
      label-width="100px"
      label-suffix=" :"
      :rules="rules"
      :disabled="drawerProps.isView"
      :model="drawerProps.row"
      :hide-required-asterisk="drawerProps.isView"
    >
      <el-form-item label="名称" prop="roleName">
        <el-input v-model="drawerProps.row!.roleName" placeholder="请填写角色名称" clearable></el-input>
      </el-form-item>
      <el-form-item label="代码" prop="roleCode">
        <el-input v-model="drawerProps.row!.roleCode" placeholder="请填写代码" clearable></el-input>
      </el-form-item>
      <el-form-item label="状态" prop="roleStateFlag">
        <el-radio v-model="roleStateFlag" label="true">启用</el-radio>
        <el-radio v-model="roleStateFlag" label="false">禁用</el-radio>
      </el-form-item>
      <el-form-item label="网络设备ip" prop="networkIp">
        <el-input v-model="drawerProps.row!.networkIp" placeholder="网络设备ip" clearable></el-input>
      </el-form-item>
      <el-form-item label="主机ip" prop="hostIp">
        <el-input v-model="drawerProps.row!.hostIp" placeholder="主机ip" clearable></el-input>
      </el-form-item>
      <el-form-item label="数据库ip" prop="dbIp">
        <el-input v-model="drawerProps.row!.dbIp" placeholder="数据库ip" clearable></el-input>
      </el-form-item>
      <el-form-item label="标准应用ip" prop="yingyong">
        <el-input v-model="drawerProps.row!.yingyong" placeholder="标准应用ip" clearable></el-input>
      </el-form-item>
      <el-form-item label="中间件ip" prop="zhongjianjian">
        <el-input v-model="drawerProps.row!.zhongjianjian" placeholder="中间件ip" clearable></el-input>
      </el-form-item>
      <el-form-item label="小程序菜单权限" v-model="list_Wechat_Resource_Home" prop="list_Wechat_Resource_Home">
        <el-checkbox v-model="list_Wechat_Resource_Home.xtjkd" label="系统健康度" size="large" />
        <el-checkbox v-model="list_Wechat_Resource_Home.erpxt" label="ERP系统" size="large" />
        <el-checkbox v-model="list_Wechat_Resource_Home.gisxt" label="GIS系统" size="large" />
        <el-checkbox v-model="list_Wechat_Resource_Home.ysxt" label="营收系统" size="large" />
        <el-checkbox v-model="list_Wechat_Resource_Home.wzxt" label="网站系统" size="large" />
        <el-checkbox v-model="list_Wechat_Resource_Home.wlaqxt" label="网络安全系统" size="large" />
        <el-checkbox v-model="list_Wechat_Resource_Home.hjzxxt" label="呼叫中心系统" size="large" />
        <el-checkbox v-model="list_Wechat_Resource_Home.btsodcl" label="BTSO待处理" size="large" />
        <el-checkbox v-model="list_Wechat_Resource_Home.yjdcl" label="邮件待处理" size="large" />
        <el-checkbox v-model="list_Wechat_Resource_Home.wlsb" label="网络设备" size="large" />
        <el-checkbox v-model="list_Wechat_Resource_Home.fwq" label="服务器" size="large" />
        <el-checkbox v-model="list_Wechat_Resource_Home.sjk" label="数据库" size="large" />
        <el-checkbox v-model="list_Wechat_Resource_Home.zjj" label="中间件" size="large" />
        <el-checkbox v-model="list_Wechat_Resource_Home.bzyy" label="标准应用" size="large" />
        <el-checkbox v-model="list_Wechat_Resource_Home.wllljc" label="网络流量监测" size="large" />
        <el-checkbox v-model="list_Wechat_Resource_Home.zntj" label="智能统计" size="large" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="drawerVisible = false">取消</el-button>
      <el-button v-show="!drawerProps.isView" type="primary" @click="handleSubmit">确定</el-button>
    </template>
  </el-drawer>
</template>

<script setup lang="ts" name="UserDrawer">
import { ref, reactive } from "vue";
import { genderType } from "@/utils/dict";
import { ElMessage, FormInstance } from "element-plus";
import { User, RoleManager } from "@/api/interface";
import UploadImg from "@/components/Upload/Img.vue";
import UploadImgs from "@/components/Upload/Imgs.vue";
import { existByRoleCode, existByRoleName, save, editRole } from "@/api/modules/accessController";
const rules = reactive({
  roleName: [{ required: true, message: "请输入名称" }],
  roleCode: [{ required: true, message: "请输入编号" }]
});

let list_Wechat_Resource_Home = reactive({
  xtjkd: false,
  erpxt: false,
  gisxt: false,
  ysxt: false,
  wzxt: false,
  wlaqxt: false,
  hjzxxt: false,
  yjdcl: false,
  wlsb: false,
  fwq: false,
  sjk: false,
  zjj: false,
  bzyy: false,
  wllljc: false,
  zntj: false,
  btsodcl: false
});

let roleStateFlag = ref("");

interface DrawerProps {
  title: string;
  isView: boolean;
  row: Partial<RoleManager.ResRoleList>;
  api?: (params: any) => Promise<any>;
  getTableList?: () => void;
}

const drawerVisible = ref(false);
const drawerProps = ref<DrawerProps>({
  isView: false,
  title: "",
  row: {}
});

// 接收父组件传过来的参数
const acceptParams = (params: DrawerProps) => {
  // 显示小程序菜单选择
  let str = params.row.list_Wechat_Resource_Home;
  if (str == null && str == "") {
    str = "";
  }
  if (str == undefined) {
    str = "";
  }
  list_Wechat_Resource_Home.xtjkd = str.includes("系统健康度") ? true : false;
  list_Wechat_Resource_Home.erpxt = str.includes("ERP系统") ? true : false;
  list_Wechat_Resource_Home.gisxt = str.includes("GIS系统") ? true : false;
  list_Wechat_Resource_Home.ysxt = str.includes("营收系统") ? true : false;
  list_Wechat_Resource_Home.wzxt = str.includes("网站系统") ? true : false;
  list_Wechat_Resource_Home.wlaqxt = str.includes("网络安全系统") ? true : false;
  list_Wechat_Resource_Home.hjzxxt = str.includes("呼叫中心系统") ? true : false;
  list_Wechat_Resource_Home.btsodcl = str.includes("BTSO待处理") ? true : false;
  list_Wechat_Resource_Home.yjdcl = str.includes("邮件待处理") ? true : false;
  list_Wechat_Resource_Home.wlsb = str.includes("网络设备") ? true : false;
  list_Wechat_Resource_Home.fwq = str.includes("服务器") ? true : false;
  list_Wechat_Resource_Home.sjk = str.includes("数据库") ? true : false;
  list_Wechat_Resource_Home.zjj = str.includes("中间件") ? true : false;
  list_Wechat_Resource_Home.bzyy = str.includes("标准应用") ? true : false;
  list_Wechat_Resource_Home.wllljc = str.includes("网络流量监测") ? true : false;
  list_Wechat_Resource_Home.zntj = str.includes("智能统计") ? true : false;
  let roleState = params.row.roleState;
  if (roleState == true || roleState == undefined) {
    roleStateFlag.value = "true";
  } else {
    roleStateFlag.value = "false";
  }
  drawerProps.value = params;
  drawerVisible.value = true;
};

// 提交数据（新增/编辑）
const ruleFormRef = ref<FormInstance>();
const handleSubmit = () => {
  ruleFormRef.value!.validate(async valid => {
    if (!valid) return;
    try {
      let param = {
        roleName: "",
        roleCode: "",
        roleState: false,
        networkIp: "",
        hostIp: "",
        dbIp: "",
        yingyong: "",
        zhongjianjian: "",
        list_Wechat_Resource_Home: "",
        companyNo: "",
        roleId: ""
      };
      param.roleName = drawerProps.value.row!.roleName == undefined ? "" : drawerProps.value.row!.roleName;
      param.roleCode = drawerProps.value.row!.roleCode == undefined ? "" : "ROLE_" + drawerProps.value.row!.roleCode;
      param.roleState = roleStateFlag.value == "true" ? true : false;
      param.networkIp = drawerProps.value.row!.networkIp == undefined ? "" : drawerProps.value.row!.networkIp;
      param.hostIp = drawerProps.value.row!.hostIp == undefined ? "" : drawerProps.value.row!.hostIp;
      param.dbIp = drawerProps.value.row!.dbIp == undefined ? "" : drawerProps.value.row!.dbIp;
      param.yingyong = drawerProps.value.row!.yingyong == undefined ? "" : drawerProps.value.row!.yingyong;
      param.zhongjianjian = drawerProps.value.row!.zhongjianjian == undefined ? "" : drawerProps.value.row!.zhongjianjian;
      param.companyNo = drawerProps.value.row!.companyNo == undefined ? "" : drawerProps.value.row!.companyNo.toString();
      param.roleId = drawerProps.value.row!.roleId == undefined ? "" : drawerProps.value.row!.roleId.toString();

      let list_Wechat_Resource_HomeStr = "";
      list_Wechat_Resource_HomeStr += list_Wechat_Resource_Home.xtjkd == true ? "系统健康度," : "";
      list_Wechat_Resource_HomeStr += list_Wechat_Resource_Home.erpxt == true ? "ERP系统," : "";
      list_Wechat_Resource_HomeStr += list_Wechat_Resource_Home.gisxt == true ? "GIS系统," : "";
      list_Wechat_Resource_HomeStr += list_Wechat_Resource_Home.ysxt == true ? "营收系统," : "";
      list_Wechat_Resource_HomeStr += list_Wechat_Resource_Home.wzxt == true ? "网站系统," : "";
      list_Wechat_Resource_HomeStr += list_Wechat_Resource_Home.wlaqxt == true ? "网络安全系统," : "";
      list_Wechat_Resource_HomeStr += list_Wechat_Resource_Home.hjzxxt == true ? "呼叫中心系统," : "";
      list_Wechat_Resource_HomeStr += list_Wechat_Resource_Home.btsodcl == true ? "BTSO待处理," : "";
      list_Wechat_Resource_HomeStr += list_Wechat_Resource_Home.yjdcl == true ? "邮件待处理," : "";
      list_Wechat_Resource_HomeStr += list_Wechat_Resource_Home.wlsb == true ? "网络设备," : "";
      list_Wechat_Resource_HomeStr += list_Wechat_Resource_Home.fwq == true ? "服务器," : "";
      list_Wechat_Resource_HomeStr += list_Wechat_Resource_Home.sjk == true ? "数据库," : "";
      list_Wechat_Resource_HomeStr += list_Wechat_Resource_Home.zjj == true ? "中间件," : "";
      list_Wechat_Resource_HomeStr += list_Wechat_Resource_Home.bzyy == true ? "标准应用," : "";
      list_Wechat_Resource_HomeStr += list_Wechat_Resource_Home.wllljc == true ? "网络流量监测," : "";
      list_Wechat_Resource_HomeStr += list_Wechat_Resource_Home.zntj == true ? "智能统计," : "";
      param.list_Wechat_Resource_Home = list_Wechat_Resource_HomeStr;

      if (`${drawerProps.value.title}` == "新增") {
        let falg = true;
        await existByRoleName({ roleName: drawerProps.value.row!.roleName }, "roleDto").then(res => {
          if (res.toString() != "true") {
            ElMessage.error({ message: "角色名称已存在" });
            falg = false;
          }
        });
        await existByRoleCode({ roleCode: "ROLE_" + drawerProps.value.row!.roleCode }, "roleDto").then(res => {
          if (res.toString() != "true") {
            ElMessage.error({ message: "编号已存在" });
            falg = false;
          }
        });
        if (falg == true) {
          await save(param, "roleDto").then(res => {
            ElMessage.success({ message: `${drawerProps.value.title}用户成功！` });
          });
          drawerProps.value.getTableList!();
          drawerVisible.value = false;
        }
      } else {
        await editRole(param, "roleDto").then(res => {
          ElMessage.success({ message: `${drawerProps.value.title}用户成功！` });
        });
        drawerProps.value.getTableList!();
        drawerVisible.value = false;
      }
    } catch (error) {
      console.log(error);
    }
  });
};

defineExpose({
  acceptParams
});
</script>
